package tal.shorer.gameoflife;

import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class GameOfLifeCell implements OnClickListener {

	private GameOfLifeActivity caller;
	private int i, j;
	private Button btn;
	private boolean state;
	
	public GameOfLifeCell(GameOfLifeActivity caller, Button btn, int i, int j) {
		this.caller = caller;
    	this.i = i;
    	this.j = j;
    	this.btn = btn;
    	this.btn.setOnClickListener(this);
    	Log.v(this.caller.getString(R.string.app_logging_tag), String.format("New cell %d %d", this.i, this.j));
    	this.state = false;
		this.rePaint();
	}
	
	@Override
	public void onClick(View v) {
    	this.state = !this.state;
    	this.rePaint();
	}
	
	public boolean getState() {
		return this.state;
	}
	
	public void setState(boolean state) {
		boolean oldState = this.state;
		this.state = state;
		if (this.state != oldState) {
			Log.v(
				this.caller.getString(R.string.app_logging_tag),
					String.format(
						"Cell %d %d switched state to %b",
						i, j, this.state
					)
			);
			this.rePaint();
		}
	}
	
	public void rePaint() {
		this.btn.setBackground(this.caller.drawables[this.state ? 1 : 0]);
	}

}
